<template>
    <div class="data-list" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="请输入您要搜索的内容"
                style="width:200px;"
                @keyup.enter.native="getList(1)"
                clearable
                v-model.trim="keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-date-picker
                @change="getList(1)"
                v-model="date"
                style="margin-left:10px;"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
            <el-select
                @change="getList(1)"
                style="margin-left:10px;width:100px;"
                v-model="platForm">
                <el-option label="全部" value="全部"></el-option>
                <el-option label="taptap" value="taptap"></el-option>
                <el-option label="应用宝" value="应用宝"></el-option>
            </el-select>
            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
            <el-button type="success" class="btn" @click="createData">新建</el-button>
        </div>
        <div class="table-box">
            <el-table
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                    fixed
                    width="50"
                    prop="id"
                    label="id">
                </el-table-column>
                <el-table-column
                    width="70"
                    prop="platform"
                    label="平台">
                </el-table-column>
                <el-table-column
                    width="180"
                    prop="task"
                    label="检索信息">
                </el-table-column>
                <el-table-column
                    width="105"
                    label="时间">
                    <template slot-scope="scope">
                        <div style="text-align:center;">{{_formatDate(scope.row.date, 'YYYY-MM-DD HH:MM')}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="100"
                    label="搜索结果">
                    <template slot-scope="scope">
                        <div>游戏：{{scope.row.selectGameCount}}个</div>
                        <div>视频：{{scope.row.selectVideoCount}}个</div>
                        <!-- <div>图片：{{scope.row.selectResult.imageSize}}张</div> -->
                    </template>
                </el-table-column>
                <el-table-column
                    width="280"
                    label="导入内容">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.gameImportCountList" :key="index" class="game-item">
                            <span class="name">游戏名称：{{item.gameName}}</span>
                            <span class="count">视频：{{item.videoCount || 0}}</span>
                        </div>
                        <div v-if="!scope.row.gameImportCountList.length">-</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="gameName"
                    fixed="right"
                    label="状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status === 0">未开始</span>
                        <span v-else-if="scope.row.status === 1" style="color: #67C23A;">导入中</span>
                        <span v-else style="color: #F56C6C;">已结束</span>
                    </template>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    label="操作">
                    <template slot-scope="scope">
                        <div><el-button @click="goDeail(scope.row.id)" type="text" size="small">查看详情</el-button></div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            keyWord: '', // 关键字
            platForm: '全部', // 平台
            tableData: [],
            date: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            dialogVisible: false
        }
    },
    created () {
        this.getList(this.pageNum)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.loading = true
            this.pageNum = pageNum
            this.listParams = {
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }
            if (this.fromDate) {
                this.listParams.fromDate = this.fromDate
            }
            if (this.date && this.date.length) {
                this.listParams.fromDate = this.date[0]
                this.listParams.toDate = this.date[1]
            }
            if (this.keyWord) {
                this.listParams.keyWord = this.keyWord
            }
            if (this.platForm !== '全部') {
                this.listParams.platForm = this.platForm
            }
            let res = await this.$http.get('/spider/gameSelectHomePage', { params: this.listParams })
            if (res.code === '0') {
                this.tableData = res.data.gameCollectionRuleResultVoList || []
                this.total = res.data.total
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 页容切换
        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        // 页码切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        // 查看视频详情
        goDeail (id) {
            this.$router.push({
                path: '/data/dataDetail',
                query: {
                    id
                }
            })
        },

        createData () {
            this.$router.push({
                path: '/data/createData'
            })
        },

        _formatDate (timestamp, type = 'YYYY-MM-DD HH:MM') {
            // return timestamp
            return formatDate(timestamp, type)
        }
    }
}
</script>

<style lang="scss" scoped>
    .data-list {
        .input-box {
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
            .game-item {
                display: flex;
                .name {
                    flex: 1;
                    overflow: hidden;
                    text-overflow:ellipsis;
                    white-space: nowrap;
                }
                .count {
                    width: 70px;
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
